//
//  UIImageView+Download.m
//  BuDeJie
//
//  Created by 仲琦 on 16/4/16.
//  Copyright © 2016年 仲琦. All rights reserved.
//

#import "UIImageView+Download.h"

#import <AFNetworking.h>
@implementation UIImageView (Download)
//  先判断, 内存中有没有图片  有图片直接赋值, 如果没有  判断是否wifi连接,
//  wifi连接下载大图,  wifi没连接 判断 手机是否连接 网络
//  手机连接了网络,判断用户配置是否下载原图, 如果没有配置 下载小图
//  手机没有联网查看缓存是否有小图. 有几赋值,没有就赋值占位图

- (void)zq_setImageOriginalImageURL:(NSString *)originalImageURL thumbnailImageURL:(NSString *)thumbnailImageURL placeholderImageURL:(UIImage *)placeholderImageURL completed:(SDWebImageCompletionBlock)completedBlock
{
//取出沙盒中的大图
UIImage *originalImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:originalImageURL];

if (originalImage){//有内存图直接复制
    [self sd_setImageWithURL:[NSURL URLWithString:originalImageURL] placeholderImage:placeholderImageURL completed:completedBlock];
}else{//没有内存图 判断是否为wifi
    //取出AFN单例
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    
    if (mgr.reachableViaWiFi) {//  是wifi状态, 直接复制
        
        [self sd_setImageWithURL:[NSURL URLWithString:originalImageURL] placeholderImage:placeholderImageURL completed:completedBlock];
        
    }else if(mgr.reachableViaWWAN){// 手机网络状态,判断用户是否允许加载大图
        
#warning 从沙盒中读取用户的配置项：在3G\4G环境是否仍然下载原图
        //获取用户配置是否加载大图
        BOOL alwaysDownloadOriginalImage = [[NSUserDefaults standardUserDefaults]boolForKey:@"alwaysDownloadOriginalImage"];
        
        if (alwaysDownloadOriginalImage) {//允许  加载大图
            [self sd_setImageWithURL:[NSURL URLWithString:originalImageURL] placeholderImage:placeholderImageURL completed:completedBlock];
        }else{//不允许   就加载缩略图
            [self sd_setImageWithURL:[NSURL URLWithString:thumbnailImageURL] placeholderImage:placeholderImageURL completed:completedBlock];
        }
        
    }else{ //无网络状态, 判断是否有缩略图
        //取出沙盒中的缩略图
        UIImage *thumbnailImage = [[SDImageCache sharedImageCache]imageFromDiskCacheForKey:thumbnailImageURL];
        
        if (thumbnailImage) {//有缩略图直接复制
            [self sd_setImageWithURL:[NSURL URLWithString:thumbnailImageURL] placeholderImage:placeholderImageURL completed:completedBlock];
        }else{ //没有缩略图,加载占位图
            [self sd_setImageWithURL:nil placeholderImage:placeholderImageURL completed:completedBlock];
            
        }
    }
}
}


- (void)zq_setImageOriginalImageURL:(NSString *)originalImageURL thumbnailImageURL:(NSString *)thumbnailImageURL placeholderImageURL:(UIImage *)placeholderImageURL
{
    [self zq_setImageOriginalImageURL:originalImageURL thumbnailImageURL:thumbnailImageURL placeholderImageURL:placeholderImageURL completed:nil];
}

- (void)zq_setImageOriginalImageURL:(NSString *)originalImageURL thumbnailImageURL:(NSString *)thumbnailImageURL
{
    [self zq_setImageOriginalImageURL:originalImageURL thumbnailImageURL:thumbnailImageURL placeholderImageURL:nil completed:nil];
}

- (void)zq_setImageOriginalImageURL:(NSString *)originalImageURL thumbnailImageURL:(NSString *)thumbnailImageURL completed:(SDWebImageCompletionBlock)completedBlock
{
    [self zq_setImageOriginalImageURL:originalImageURL thumbnailImageURL:thumbnailImageURL placeholderImageURL:nil completed:completedBlock];
}
@end
